package figure8.GUI;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.File;
import java.util.ArrayList;

import javax.sound.midi.Instrument;
import javax.sound.midi.InvalidMidiDataException;
import javax.sound.midi.MidiUnavailableException;
import javax.sound.sampled.Mixer;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JToggleButton;
import javax.swing.border.Border;

import figure8.Controller;
import figure8.DrumMachineTrack;
import figure8.LoopTiming;
import figure8.Project;
import figure8.Rec;
import figure8.Seq;

public class SeqMainWindow extends JFrame {
	private int selectedItem;
	private LoopTiming timings;
	private Controller controller;
	private File[] drums;
	private int i, j ,x;
	private DrumMachineTrack drumMachine;
	private Seq sequencer;
	private ArrayList<JPanel> loops;
	private seqMenuBar menuBar;
	private JProgressBar progress = new JProgressBar(0, 100);
	private JPanel main, seq, buttons;
	private GridLayout mainGrid, seqGrid, buttonGrid;
	
	public SeqMainWindow (final Controller controller, final Seq sequencer) {
		super();
		this.addWindowListener(new WindowAdapter() 
		{
		  public void windowClosing(WindowEvent e)
		  {
			  if(sequencer.getSequence().size() > 0) {
				  NotSavedWindow warningWin = new NotSavedWindow(sequencer);
			  }
			  else {
				  System.exit(0);
			  }
		  }
		});
		menuBar = new seqMenuBar(sequencer);
		this.setJMenuBar(menuBar);
		this.setIconImage(Toolkit.getDefaultToolkit().getImage("src/figure8/GUI/Fig8-small.png"));
		setLayout(new BorderLayout());
		this.add(progress, BorderLayout.NORTH);
		progress.setValue(0);
		loops = new ArrayList<JPanel>();
		this.sequencer = sequencer;
		this.setLocation(controller.getWindow().getFrame().getX() + 80, controller.getWindow().getFrame()
				.getY() + 60);
		this.setPreferredSize(new Dimension(900, 200));
		this.controller = controller;
		mainGrid = new GridLayout(2, 0);
		seqGrid = new GridLayout();
		buttonGrid = new GridLayout(0, 3);
		main = new JPanel();
	    main.setLayout(mainGrid);
		seq = new JPanel();
		seq.setLayout(seqGrid);
	    buttons = new JPanel();
	    buttons.setLayout(buttonGrid);
		final JButton add = new JButton("Add Loop");
		final JFrame frame = this;
		add.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				JFileChooser openFC = new JFileChooser();
				openFC.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
				//JFrame frame = new JFrame();
				int returnVal = openFC.showDialog(frame, "Open");
				if (returnVal == JFileChooser.APPROVE_OPTION) {
					String dir = openFC.getSelectedFile().getAbsolutePath();
					sequencer.projectBuilder(dir);
					addLoopPane();
				}
			}
		});
		final JButton play = new JButton("Play");
		buttons.add(play);
		play.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				sequencer.play();
			}
		});
		final JButton stop = new JButton("Stop");
		buttons.add(stop);
		stop.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				sequencer.stop();
			}
		});
		buttons.add(add);
	    seq.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
	    buttons.setBorder(BorderFactory.createEmptyBorder(15, 10, 10, 10));
	    main.add(seq);
	    main.add(buttons);
	    this.add(main);
		this.pack();
		this.setVisible(true);
	}
	
	public void addLoopPane() {
		JPanel panel = new JPanel();
		int hash = sequencer.getLastProjectName().hashCode() % 255;
		if (hash < 0) hash *= -1;
		Color c = new Color(100, hash, (hash * 2) % 255, 75);
		panel.setBackground(c);
		JLabel loop = new JLabel();
		loop.setText(sequencer.getLastProjectName());
		panel.add(loop);
		final JButton delete = new JButton();
		delete.setText("D");
		final int index = loops.size();
		final Project project = sequencer.getLastProject();
		delete.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				deleteLoop(project);
			}
		});
		panel.add(delete);
		panel.setBorder(BorderFactory.createEmptyBorder(0, 10, 0, 10));
		loops.add(panel);
		seq.add(panel);
		progress.setMaximum(loops.size());
		refresh();
	}
	
	
	public void deleteLoop(Project project) {
		int index = sequencer.indexOfProject(project);
		System.out.println("Delete to be deleted: " + index);
		loops.remove(index);
		progress.setMaximum(loops.size());
		this.sequencer.deleteLoop(project);
		deletePanel();
	}
	
	public void nextLoop() {
		progress.setValue(progress.getValue() + 1);
	}
	
	public void showCurrentLoop(int index) {
		int hash = sequencer.getProjectName(index).hashCode() % 255;
		Color c = new Color(100, hash, (hash * 2) % 255, 110);
		loops.get(index).setBackground(c);
		/*if (index > 0) {
			hash = sequencer.getProjectName(index - 1).hashCode() % 255;
			c = new Color(100, hash, (hash * 2) % 255, 75);
			loops.get(index - 1).setBackground(c);
		}*/
	}
	
	public void stopped () {
		/*for (int index = 0; index < loops.size(); index++) {
			loops.remove(index);
			int hash = sequencer.getProjectName(index).hashCode() % 255;
			Color c = new Color(100, hash, (hash * 2) % 255, 75);
			JPanel panel = new JPanel();
			panel.setBackground(c);
			JLabel label = new JLabel();
			label.setText(sequencer.getProjectName(index));
			panel.add(label);
			loops.add(panel);
		}*/
		progress.setValue(0);
	}
	
	public void deletePanel() {
		this.remove(main);
		main = new JPanel();
		main.setLayout(mainGrid);
		main.remove(seq);
		main.remove(buttons);
		seq = new JPanel();
		seq.setLayout(seqGrid);
		for (int i = 0; i < loops.size(); i++) {
			seq.add(loops.get(i));
		}
		main.setLayout(mainGrid);
	    seq.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
		main.add(seq);
		main.add(buttons);
		this.add(main);
		this.pack();
		this.setVisible(true);
	}
	
	public void refresh() {

		this.pack();
		this.setVisible(true);
	}

	public JPanel getPanel() {
		return main;
	}

}